package com.changgou.search.service.impl;

import com.alibaba.fastjson.JSON;
import com.changgou.entity.Result;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.pojo.Sku;
import com.changgou.search.dao.ImportMapper;
import com.changgou.search.pojo.SkuInfo;
import com.changgou.search.service.ImportService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 数据导入业务逻辑层
 *
 * @author gaofeng
 * @date 2025年04月23日 12:58
 */
@Service("searchServiceImpl")
public class ImportServiceImpl implements ImportService {
    @Resource
    private ImportMapper searchMapper;

    @Resource
    private SkuFeign skuFeign;

    @Override
    public void importDataList() {
        Result<List<Sku>> skus = skuFeign.findAll();
        List<SkuInfo> skuInfos = JSON.parseArray(JSON.toJSONString(skus.getData()), SkuInfo.class);
        for(SkuInfo skuInfo:skuInfos){
            Map<String, Object> specMap= JSON.parseObject(skuInfo.getSpec()) ;
            skuInfo.setSpecMap(specMap);
        }

        searchMapper.saveAll(skuInfos);
    }
}
